package cn.com.shadowless.xinning.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mengpeng.mphelper.ToastUtils;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.AssetRegisterCardInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;

/**
 * The type Asset register card fragment.
 *
 * @author sHadowLess
 */
public class AssetRegisterCardFragment extends BaseFragment {

    /**
     * The Register order no.
     */
    @BindView(R.id.register_orderNo)
    TextView registerOrderNo;
    /**
     * The Register make time.
     */
    @BindView(R.id.register_makeTime)
    TextView registerMakeTime;
    /**
     * The Register maker.
     */
    @BindView(R.id.register_maker)
    TextView registerMaker;
    /**
     * The Register position.
     */
    @BindView(R.id.register_position)
    TextView registerPosition;
    /**
     * The Register depart name.
     */
    @BindView(R.id.register_departName)
    TextView registerDepartName;
    /**
     * The Register applier.
     */
    @BindView(R.id.register_applier)
    TextView registerApplier;
    /**
     * The Register carriers.
     */
    @BindView(R.id.register_carriers)
    TextView registerCarriers;
    /**
     * The Register models.
     */
    @BindView(R.id.register_models)
    TextView registerModels;
    /**
     * The Register unit.
     */
    @BindView(R.id.register_unit)
    TextView registerUnit;
    /**
     * The Register origin cost.
     */
    @BindView(R.id.register_originCost)
    TextView registerOriginCost;
    /**
     * The Register depots.
     */
    @BindView(R.id.register_depots)
    TextView registerDepots;
    /**
     * The Register buyer.
     */
    @BindView(R.id.register_buyer)
    TextView registerBuyer;
    /**
     * The Register remark.
     */
    @BindView(R.id.register_remark)
    TextView registerRemark;
    /**
     * The Register check process.
     */
    @BindView(R.id.register_check_process)
    LinearLayout registerCheckProcess;

    private final String url = "api/FixedAssets/LoadCheckHistory?%s=";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_asset_register_layout;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            initRegisterInfo(orderId, userName);
            initCheckHistoryList(String.format(url, "id") + orderId, mActivity, registerCheckProcess);
        }
    }

    private void initRegisterInfo(String orderId, String userName) {
        OrderPresenter
                .getSingleton()
                .getAssetRegisterCardInfo(orderId, userName, new RequestCallBack<AssetRegisterCardInfo>() {
                    @Override
                    public void success(RequestResult<AssetRegisterCardInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                Log.e(TAG, "success: " + result.toString());
                                AssetRegisterCardInfo.DataBean dataBean = result.getResult().getData();
                                AssetRegisterCardInfo.DataBean.AssetBean assetBean = dataBean.getAsset();
                                String id = dataBean.getId();
                                String createTimeStr = dataBean.getCreateTimeStr();
                                String createUserName = dataBean.getCreateUserName();
                                String createUserDepartmentName = dataBean.getCreateUserDepartmentName();
                                registerOrderNo.setText(id);
                                registerMakeTime.setText(createTimeStr);
                                registerMaker.setText(createUserName);
                                registerPosition.setText(createUserDepartmentName);

                                String ownerDepartmentName = assetBean.getOwnerDepartmentName();
                                String ownerName = assetBean.getOwnerName();
                                String assetName = assetBean.getAssetName();
                                String modelNumber = assetBean.getModelNumber();
                                String supplier = assetBean.getSupplier();
                                String originalValue = getDecimal(assetBean.getOriginalValue().doubleValue());
                                String storageLocation = assetBean.getStorageLocation();
                                String buyerName = assetBean.getBuyerName();
                                String remark = assetBean.getRemark();
                                registerDepartName.setText(ownerDepartmentName);
                                registerApplier.setText(ownerName);
                                registerCarriers.setText(assetName);
                                registerModels.setText(modelNumber);
                                registerUnit.setText(supplier);
                                registerOriginCost.setText(originalValue);
                                registerDepots.setText(storageLocation);
                                registerBuyer.setText(buyerName);
                                registerRemark.setText(remark);
                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("该订单暂无详细信息");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }

    @Override
    protected void initView() {

    }


}
